<?php
namespace Wap\Controller;
 
class MemberController extends CommonController {
	protected $user = null;
	
	protected function getUser() {
		if($this -> user === null) {
			$this -> user = M($this -> tm_user) -> where('id = ' . $_SESSION['boboid']);
		}
		return $this -> user;
	}
	
	/**
	 *	用户首页
	 */
	public function index() {
		if(isset($_SESSION['boboid']) && isset($_SESSION['user_id'])) {
			$this -> assign('integral', M($this -> tm_integral) ->  where('userid = ' . $_SESSION['user_id']) -> find());
		}
		$this -> display();
	}
	
	/**
	 *	用户注册
	 */
	public function register() {
		if(IS_POST) {
			$data = I('post.');
			$data['status'] = 1;
			$data['password'] = _enPasswd(md5($data['password']));
			$data['createtime'] = time();
			$data['sex'] = 3;
			$data['isemail'] = 0;
			$data['last_ip'] = getClientIP();
			$data['last_time'] = $data['createtime'];
			$data['current_ip'] = getClientIP();
			$data['current_time'] = $data['createtime'];
			if(M($this -> tm_user) -> add($data)) {
				$this -> success('注册成功', 'login');
			} else {
				$this -> error('注册失败');
			}
			exit;
		}
		$this -> display();
	}

	/**
	 *	用户登陆
	 */
	public function login() {
		if(IS_POST) {
			$user = new \Home\Aip\User();
			$uinfo = $user -> _login();
			if ($uinfo=='boboid') {
				 $this->redirect('index','登录成功');
			} elseif ($uinfo=='ok') {
				$this->redirect('index','登录成功');
			} else{
				$this->error($uinfo);
			}
		}
		$this -> display();
	}

	/**
	 *	用户退出
	 */
	public function logout() {
		session(null);
		$this->redirect('index');
	}
	
	/**
	 *	用户忘记密码
	 */
	public function forget() {
		if(IS_POST) {
			$data = I('post.');
		}
		$this -> display();
	}
	
	/**
	 * 用户信息
	 */
	public function info() {
		if($this -> isLogin() && IS_POST) {
			$data = I('post.');
			if($this -> getUser() -> save($data)) {
			$this -> success('修改成功', 'index');
				exit;
			} else {
				$this -> error('修改失败');
			}
		}
		$this -> assign('user_data', $this -> getUser() -> find());
		$this -> display();
	}

	/**
	 * 用户地址
	 */
	public function address() {
		$this -> isLogin();
		$list = M($this -> tm_address)->where(array('userid'=>$_SESSION['user_id'])) -> select();
		if(IS_POST) {
			$arr = array();
			foreach($list as $k => $v) {
				$arr[$k]['value'] = $v['id'];
				$arr[$k]['text'] = $v['realname'] . ',' . $v['mob_phone'] . ',' . $v['province'] . ',' . $v['city'] . ',' . $v['area'] . ',' . $v['address'];
			}
			echo json_encode($arr);exit;
		}
		$this -> assign('list',$list);
		$this -> display();
	}

	/**
	 * 地址添加
	 */
	public function add_address() {
		$id = I('get.id')?I('get.id'):'';
		$address = M($this -> tm_address);
		if($this -> isLogin() && IS_POST) {
			$data = I('post.');
			if($data['op'] == 'delete') {
				if($address -> where('id = ' . (int)$data['id']) -> delete()) {
					echo json_encode(array('code' => 0));
				} else {
					echo json_encode(array('code' => 1, 'message' => '删除失败！'));
				}
				exit;
			}
			unset($id);
			$id = $data['id'];
			unset($data['id']);
			$a = explode(' ', $data['city']);
			unset($data['city']);
			$data['province'] = $a[0];
			$data['city'] = $a[1];
			$data['area'] = $a[2];
			$data['is_default'] = 0;
			if(empty($id)) {
				$data['userid'] = $_SESSION['user_id'];
				$result = $address -> add($data);
			} else {
				$result = $address -> where(array('id'=>$id)) -> save($data);
			}
			if($result) {
				$this -> success('成功', 'address');
				exit;
			} else {
				$this -> error('失败');
			}
		}
		if(!empty($id)) {
			$this -> assign('address',$address ->where(array('id'=>$id)) -> find());
		}
		$this -> assign('id', $id);
		$this -> display();
	}
	
	/**
	 * 修改登录密码
	 */
	public function change_password() {
		$this -> change('password');
		$this -> display();
	}

	/**
	 * 修改支付密码
	 */
	public function change_pay() {
		$this -> change('paypassword');
		$this -> display();
	}

	protected function change($str) {
		if($this -> isLogin() && IS_POST) {
			$data = I('post.');
			if($data['password_old'] == $data['password_new'] ||$data['password_old'] == $data['passowrd_new_re']) {
				$this -> error('旧密码不能与新密码一样');
			}
			if($data['password_new'] != $data['passowrd_new_re']) {
				$this -> error('您两次输入的密码不一样');
			}
			$this -> getUser();
			$user_data = $this -> getUser() -> find();
			if(!_checkPwd(md5($data['password_old']), $user_data[$str])) {
				$this -> error('您输入的密码有误');
			}
			$this -> getUser() -> $str = _enPasswd(md5($data['password_new']));
			if($this -> getUser() -> save()) {
				$this -> success('修改成功', 'index');
				exit;
			} else {
				$this -> error('修改失败');
			}
		}
	}
	/**
	 * 用户收藏产品
	 */
	public function favoritep() {
		$this -> isLogin();
		$collects = M('collect');
		$collects -> join('tm_product ON tm_product.id = tm_collect.sc_id');
		$collects -> field('tm_product.id as g_id, tm_product.productname, tm_product.pic, tm_product.price, tm_product.st_id, tm_collect.id as c_id');
		$collects -> where('tm_collect.userid = ' . $_SESSION['user_id'] . ' and tm_collect.sc_lx = "sp"');
		$collects = $collects -> limit(10) -> select();

		$this -> assign('collects', $collects);
		$this -> display();
	}

	/**
	 * 用户收藏店铺
	 */
	public function favorited() {
		$this -> isLogin();
		$collects = M('collect');
		$collects -> join('tm_store ON tm_store.store_id = tm_collect.sc_id');
		$collects -> field('tm_store.store_id as st_id, tm_store.store_name, tm_store.store_label, tm_store.store_description, tm_collect.id as c_id');
		$collects -> where('tm_collect.userid = ' . $_SESSION['user_id'] . ' and tm_collect.sc_lx = "dp"');
		$collects = $collects -> limit(10) -> select();
		$this -> assign('collects', $collects);
		$this -> display();
	}
}